package com.chat.admin.service.impl;

import com.chat.admin.service.ClientBasicService;
import com.chat.admin.utils.RedisUtil;
import com.chat.common.common.constant.RedisKeyConstant;
import com.chat.common.pojo.ClientBasic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ClientBasicServiceImpl implements ClientBasicService {
    @Autowired
    RedisUtil redisUtil;

    @Override
    public ClientBasic getClientBasic() {
        // 从Redis中获取设置
        Map<Object, Object> properties = redisUtil.hGetAll(RedisKeyConstant.CLIENT_BASIC_SETTING);
        if (properties != null) {
            // 使用检索到的值构造一个新的ClientBasic对象
            return new ClientBasic(
                    (String) properties.get("version"),
                    (String) properties.get("max_friends"),
                    (String) properties.get("max_groups"),
                    (String) properties.get("max_group_members"),
                    (String) properties.get("max_message_length"),
                    (String) properties.get("max_message_size")
            );
        }
        return null;
    }

    @Override
    public Integer setClientBasic(ClientBasic clientBasic) {
        // 确认clientBasic对象非空
        if (clientBasic != null) {
            Map<String, String> map = new HashMap<>();
            map.put("version", clientBasic.getVersion());
            map.put("max_friends", clientBasic.getMaxFriends());
            map.put("max_groups", clientBasic.getMaxGroups());
            map.put("max_group_members", clientBasic.getMaxGroupMembers());
            map.put("max_message_length", clientBasic.getMaxMessageLength());
            map.put("max_message_size", clientBasic.getMaxMessageSize());

            // 将设置存入Redis
            redisUtil.hPutAll(RedisKeyConstant.CLIENT_BASIC_SETTING, map);
            return 1;
        }
        return 0;
    }
}
